/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.client.controller;

import com.bullionxpress.client.application.ClientGlobals;
import com.bullionxpress.client.view.ChartDataLoader;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JDialog;
import com.bullionxpress.client.view.CloseAccountPanel;
import com.bullionxpress.client.view.DepositPanel;
import com.bullionxpress.client.view.ModifyProfilePanel;
import com.bullionxpress.shared.model.Command;
import com.bullionxpress.client.view.OrderEntryPanel;
import com.bullionxpress.client.view.ReportView;
import com.bullionxpress.client.view.RequestChequePanel;
import com.bullionxpress.client.view.RequestDeliveryPanel;
import com.bullionxpress.client.view.WithdrawalPanel;
import com.bullionxpress.shared.application.SharedGlobals;
import com.bullionxpress.shared.model.Instrument;
import com.bullionxpress.shared.util.Log;
import java.awt.Desktop;
import java.net.URI;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 *
 * @author BullionXpress.com
 */
public class CommandController implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");

        String command = e.getActionCommand();
        Log.info("Command:" + command);

        if (command.equals(Command.DISPLAY_ORDER_ENTRY.toString())) {
            doDisplayOrderEntry();
        } else if( command.equals(Command.DISPLAY_MODIFY_PROFILE.toString())){
            doDisplayModifyProfile();
        } else if( command.equals(Command.ADD_FUNDS_REQUEST.toString())){
            doDisplayAddFunds();
        } else if (command.equals(Command.CLOSE_ACCOUNT.toString())) {
            doCloseAccount();
        } else if (command.equals(Command.REQUEST_CHEQUE.toString())) {
            doRequestCheque();
        } else if (command.equals(Command.REQUEST_DELIVERY.toString())) {
            doRequestDelivery();
        } else if( command.equals(Command.HELP.toString())){
            doDisplayHelp();
        } else if( command.equals(Command.ABOUT.toString())){
            doDisplayAbout();
        } else if( command.equals(Command.ADMIN_OPTIONS.toString())){
            doDisplayAdminOptions();
        } else if( command.equals(Command.SELECT_GOLD.toString())){
            doSelectGold();
        } else if( command.equals(Command.SELECT_SILVER.toString())){
            doSelectSilver();
        } else if( command.equals(Command.SELECT_BTC.toString())){
            doSelectBTC();
        } 
    }
    
    public void doSelectGold() 
    {
        ClientGlobals.instrument = new Instrument("GOLD");
        ChartDataLoader worker = new ChartDataLoader();
        worker.start();        
    }

    public void doSelectSilver() 
    {
        ClientGlobals.instrument = new Instrument("SILVER");        
    }
    
    public void doSelectBTC() 
    {
        ClientGlobals.instrument = new Instrument("BTC");        
    }
    
    public void doDisplayAdminOptions()
    {
        try {
            final JDialog dialog = new JDialog();
            JTabbedPane tp = new JTabbedPane();
            WithdrawalPanel wp = new WithdrawalPanel(dialog);
            DepositPanel dp = new DepositPanel(dialog);
            ReportView rp = new ReportView(dialog);
            tp.add("Deposits", dp);
            tp.add("Withdrawals", wp);
            tp.add("Reports", rp);
            dialog.setTitle("Admin Options");
            dialog.setContentPane(tp);
            dialog.setSize(new Dimension(470, 320));
            dialog.setResizable(false);
            dialog.addWindowListener(
                    new WindowListener() {

                        public void windowActivated(WindowEvent arg0) {
                        }

                        public void windowClosed(WindowEvent arg0) {
                        }

                        public void windowClosing(WindowEvent arg0) {
                            dialog.dispose();
                        }

                        public void windowDeactivated(WindowEvent arg0) {
                        }

                        public void windowDeiconified(WindowEvent arg0) {
                        }

                        public void windowIconified(WindowEvent arg0) {
                        }

                        public void windowOpened(WindowEvent arg0) {
                        }
                    });

            //Center the frame window
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = dialog.getSize();

            if (frameSize.height > screenSize.height) {
                frameSize.height = screenSize.height;
            }

            if (frameSize.width > screenSize.width) {
                frameSize.width = screenSize.width;
            }

            dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
            dialog.setModal(true);
            dialog.setVisible(true);

        } catch(Exception ex){

        }
    }
    
    public void doDisplayAddFunds()
    {
        try {
            Desktop.getDesktop().browse(new URI("http://www.bullionxpress.com/deposit.jsp?account=" + ClientGlobals.account.getId()));
        } catch (Exception ex) {
            Log.info(ex);
        }        
    }

    public void doDisplayAbout()
    {
        JOptionPane.showMessageDialog(null, "Copyright (c) 2011. BullionXpress.com [" + SharedGlobals.version + "]");
    }

    public void doDisplayHelp()
    {
         try {
            Desktop.getDesktop().browse(new URI("http://www.bullionxpress.com/help.jsp"));
        } catch (Exception ex) {
            Log.info(ex);
        }
    }

    public void doDisplayOrderEntry() {
        final JDialog dialog = new JDialog();
        OrderEntryPanel lp = new OrderEntryPanel(dialog);
        dialog.setTitle("Order Entry");
        dialog.setContentPane(lp);
        dialog.setSize(new Dimension(270, 320));
        dialog.setResizable(false);
        dialog.addWindowListener(
                new WindowListener() {

                    public void windowActivated(WindowEvent arg0) {
                    }

                    public void windowClosed(WindowEvent arg0) {
                    }

                    public void windowClosing(WindowEvent arg0) {
                        dialog.dispose();
                    }

                    public void windowDeactivated(WindowEvent arg0) {
                    }

                    public void windowDeiconified(WindowEvent arg0) {
                    }

                    public void windowIconified(WindowEvent arg0) {
                    }

                    public void windowOpened(WindowEvent arg0) {
                    }
                });

        //Center the frame window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = dialog.getSize();

        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }

        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }

        dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        dialog.setModal(true);
        dialog.setVisible(true);
    }

    public void doDisplayModifyProfile() {
        final JDialog dialog = new JDialog();
        ModifyProfilePanel lp = new ModifyProfilePanel(dialog);
        dialog.setTitle("Modify Profile");
        dialog.setContentPane(lp);
        dialog.setSize(new Dimension(500, 585));
        dialog.setResizable(false);
        dialog.addWindowListener(
                new WindowListener() {

                    public void windowActivated(WindowEvent arg0) {
                    }

                    public void windowClosed(WindowEvent arg0) {
                    }

                    public void windowClosing(WindowEvent arg0) {
                        dialog.dispose();
                    }

                    public void windowDeactivated(WindowEvent arg0) {
                    }

                    public void windowDeiconified(WindowEvent arg0) {
                    }

                    public void windowIconified(WindowEvent arg0) {
                    }

                    public void windowOpened(WindowEvent arg0) {
                    }
                });

        //Center the frame window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = dialog.getSize();

        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }

        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }

        dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        dialog.setModal(true);
        dialog.setVisible(true);
    }

    public void doRequestDelivery() {
        final JDialog dialog = new JDialog();
        RequestDeliveryPanel lp = new RequestDeliveryPanel(dialog);
        dialog.setTitle("Request Delivery");
        dialog.setContentPane(lp);
        dialog.setSize(new Dimension(270, 405));
        dialog.setResizable(false);
        dialog.addWindowListener(
                new WindowListener() {

                    public void windowActivated(WindowEvent arg0) {
                    }

                    public void windowClosed(WindowEvent arg0) {
                    }

                    public void windowClosing(WindowEvent arg0) {
                        dialog.dispose();
                    }

                    public void windowDeactivated(WindowEvent arg0) {
                    }

                    public void windowDeiconified(WindowEvent arg0) {
                    }

                    public void windowIconified(WindowEvent arg0) {
                    }

                    public void windowOpened(WindowEvent arg0) {
                    }
                });

        //Center the frame window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = dialog.getSize();

        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }

        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }

        dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        dialog.setModal(true);
        dialog.setVisible(true);

    }

    public void doRequestCheque() {
        final JDialog dialog = new JDialog();
        RequestChequePanel lp = new RequestChequePanel(dialog);
        dialog.setTitle("Request Cheque");
        dialog.setContentPane(lp);
        dialog.setSize(new Dimension(270, 300));
        dialog.setResizable(false);
        dialog.addWindowListener(
                new WindowListener() {

                    public void windowActivated(WindowEvent arg0) {
                    }

                    public void windowClosed(WindowEvent arg0) {
                    }

                    public void windowClosing(WindowEvent arg0) {
                        dialog.dispose();
                    }

                    public void windowDeactivated(WindowEvent arg0) {
                    }

                    public void windowDeiconified(WindowEvent arg0) {
                    }

                    public void windowIconified(WindowEvent arg0) {
                    }

                    public void windowOpened(WindowEvent arg0) {
                    }
                });

        //Center the frame window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = dialog.getSize();

        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }

        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }

        dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        dialog.setModal(true);
        dialog.setVisible(true);

    }

    public void doCloseAccount() {
        final JDialog dialog = new JDialog();
        CloseAccountPanel lp = new CloseAccountPanel(dialog);
        dialog.setTitle("Close Account");
        dialog.setContentPane(lp);
        dialog.setSize(new Dimension(270, 300));
        dialog.setResizable(false);
        dialog.addWindowListener(
                new WindowListener() {

                    public void windowActivated(WindowEvent arg0) {
                    }

                    public void windowClosed(WindowEvent arg0) {
                    }

                    public void windowClosing(WindowEvent arg0) {
                        dialog.dispose();
                    }

                    public void windowDeactivated(WindowEvent arg0) {
                    }

                    public void windowDeiconified(WindowEvent arg0) {
                    }

                    public void windowIconified(WindowEvent arg0) {
                    }

                    public void windowOpened(WindowEvent arg0) {
                    }
                });

        //Center the frame window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = dialog.getSize();

        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }

        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }

        dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        dialog.setModal(true);
        dialog.setVisible(true);

    }
}
